#include "shmem.h"
#include "cJSON.h"
#include "custom_handle.h"
#include "thttpd.h"
#include "msg_queue_peer.h"


struct data_shm
{
    int data_int_bool[7];
    float data_float[4];
};

char data_html[1024];

int main(int argc, char const *argv[])
{
    struct data_shm data_coll;

    // 获取共享内存数据
    struct shm_param para;
    // 初始化共享内存
    shm_init(&para, "recv_read_shm.c", 1024);
    // 取出数据
    char *p_shm = shm_getaddr(&para);
    p_shm = "{\"version\":\"v1.0\",\"report\":{\"type\":0,\"period\":5},\"stm32\":{\"data\":[{\"key\":101,\"name\":\"Temp\",\"type\":3,\"value\":20.300000,\"newval\":0},{\"key\":102,\"name\":\"Door\",\"type\":1,\"value\":1,\"newval\":0},{\"key\":103,\"name\":\"Led1\",\"type\":1,\"value\":0,\"newval\":0},{\"key\":104,\"name\":\"Led2\",\"type\":1,\"value\":0,\"newval\":0},{\"key\":105,\"name\":\"Fan\",\"type\":1,\"value\":0,\"newval\":0},{\"key\":106,\"name\":\"Ekey\",\"type\":1,\"value\":0,\"newval\":0},{\"key\":107,\"name\":\"Hum\",\"type\":3,\"value\":20.300000,\"newval\":20.300000}]},\"modbus\":{\"data\":[{\"key\":201,\"name\":\"Etemp\",\"addr\":30001,\"type\":3,\"value\":20.300000,\"newval\":0},{\"key\":202,\"name\":\"Carbon\",\"addr\":30001,\"type\":3,\"value\":0.040000,\"newval\":0},{\"key\":203,\"name\":\"Revolution\",\"addr\":40001,\"type\":2,\"value\":0,\"newval\":0},{\"key\":204,\"name\":\"Mdoor\",\"addr\":40000,\"type\":1,\"value\":1,\"newval\":0}]}}";

    

    // 解析数据
    cJSON *root;
    root = cJSON_Parse(p_shm);
    if (!root)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "ERROR before :%s\n", error_ptr);
        }
        return 1;
    }

    // 解析stm32数据
    cJSON *stm32 = cJSON_GetObjectItem(root, "stm32");
    cJSON *stm32_data = cJSON_GetObjectItem(stm32, "data");

    // 遍历数据项
    for (int i = 0; i < cJSON_GetArraySize(stm32_data); i++)
    {
        cJSON *item = cJSON_GetArrayItem(stm32_data, i);
        cJSON *key = cJSON_GetObjectItem(item, "key");
        cJSON *newval = cJSON_GetObjectItem(item, "newval");
        switch (key->valueint)
        {
        case 101:
            data_coll.data_float[0] = newval->valuedouble;
            break;
        case 102:
            data_coll.data_int_bool[0] = newval->valueint;
            break;
        case 103:
            data_coll.data_int_bool[1] = newval->valueint;
            break;
        case 104:
            data_coll.data_int_bool[2] = newval->valueint;
            break;
        case 105:
            data_coll.data_int_bool[3] = newval->valueint;
            break;
        case 106:
            data_coll.data_int_bool[4] = newval->valueint;
            break;
        case 107:
            data_coll.data_float[1] = newval->valuedouble;
            break;

        default:
            break;
        }
    }
    
    //解析modbus数据

    cJSON *modbus = cJSON_GetObjectItem(root, "modbus");
    cJSON *modbus_data = cJSON_GetObjectItem(modbus, "data");

    // 遍历数据项
    for (int i = 0; i < cJSON_GetArraySize(modbus_data); i++)
    {
        cJSON *item = cJSON_GetArrayItem(modbus_data, i);
        cJSON *key = cJSON_GetObjectItem(item, "key");
        cJSON *newval = cJSON_GetObjectItem(item, "newval");
        switch (key->valueint)
        {
        case 201:
            data_coll.data_float[2] = newval->valuedouble;
            break;
        case 202:
            data_coll.data_float[3] = newval->valueint;
            break;
        case 203:
            data_coll.data_int_bool[5] = newval->valueint;
            break;
        case 204:
            data_coll.data_int_bool[6] = newval->valueint;
            break;
        

        default:
            break;
        }
    }
    // 打印data_int_bool数组    
    for (int i = 0; i < 7; i++)
    {
        printf("data_int_bool[%d]:%d\n", i, data_coll.data_int_bool[i]);
    }
    // 打印data_float数组

    for (int i = 0; i < 4; i++)
    {
        printf("data_float[%d]:%.2f\n", i, data_coll.data_float[i]);
    }  

    //拼接到data_html字符串数组中
    snprintf(data_html, sizeof(data_html), "%.2f°C/%d/%d/%d/%d/%d/%.2f/%.2f°C/%.2f/%d/%d/", 
    //stm32
    data_coll.data_float[0], 
    data_coll.data_int_bool[0],
    data_coll.data_int_bool[1], 
    data_coll.data_int_bool[2],
    data_coll.data_int_bool[3],
    data_coll.data_int_bool[4],
    data_coll.data_float[1],
    //modbus
    data_coll.data_float[2],
    data_coll.data_float[3],
    data_coll.data_int_bool[5],
    data_coll.data_int_bool[6]);

    //打印data_html字符串数组
    printf("data_html:%s\n", data_html);


    cJSON_Delete(root);

    return 0;
}
